/*
 * @Author: your name
 * @Date: 2021-04-03 17:26:33
 * @LastEditTime: 2021-04-22 15:32:16
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \chronic-back-end\main.go
 */
package main

import (
	"chronic/common"
	"chronic/module"
	"chronic/module/consultMod"
	"chronic/module/healthMod"
	"chronic/module/pushMod"
	"chronic/module/recordMod"
	"chronic/module/reserveMod"
	"chronic/module/userMod"
	"fmt"
	"os"
	"time"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"github.com/joho/godotenv"
)

func main() {
	initEnv()
	initDB()
	bootstrap()
}

func bootstrap() {
	hello := os.Getenv("HELLO")
	fmt.Println(hello)
	app := gin.Default()
	// config := cors.DefaultConfig()
	// config.AllowAllOrigins = true
	// app.Use(cors.Default())
	app.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"*"},
		AllowMethods:     []string{"PUT", "PATCH", "GET", "PUT", "DELETE"},
		AllowHeaders:     []string{"Origin", "Authorization"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
		AllowOriginFunc: func(origin string) bool {
			return origin == "*"
		},
		MaxAge: 12 * time.Hour,
	}))
	module.Resolve(app)

	app.GET("/", func(c *gin.Context) {
		c.JSON(200,
			gin.H{
				"hello": hello,
			})
	})
	appErr := app.Run(":9090")
	fmt.Println(appErr)
}

func initDB() {
	common.DbInit()
	common.Db.AutoMigrate(&userMod.User{}, &consultMod.Consultation{}, &pushMod.Push{}, &reserveMod.Reserve{}, &reserveMod.ReserveQuota{}, &recordMod.Record{}, &healthMod.Health{})
}

func initEnv() {
	envErr := godotenv.Load()
	fmt.Println(envErr)
}
